import Vue from 'vue'
import VueRouter from 'vue-router'
import Admin from '@views/Admin'
import { isLogin } from '@utils/cookie'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/admin',
    meta: {
      needLogin: true,
      roles: ['admin', 'a', 'b']
    }
  },
  {
    path: '/admin',
    name: 'admin',
    component: Admin,
    redirect: '/admin/dashBoard',
    children: [
      {
        path: '/admin/dashBoard',
        name: '仪表盘',
        meta: {
          isNav: true,
          needLogin: true,
          roles: ['admin', 'a', 'b']
        },
        icon: 'dashboard',
        component: () => import('@views/DashBoard')
      },
      {
        path: '/admin/itemAdd',
        name: '增加页',
        meta: {
          isNav: false,
          needLogin: true,
          roles: ['admin', 'a', 'b']
        },
        component: () => import('@views/ItemAdd')
      },
      {
        path: '/admin/itemLists',
        name: '列表页',
        meta: {
          isNav: true,
          needLogin: true,
          roles: ['admin', 'a', 'b']
        },
        icon: 'unordered-list',
        component: () => import('@views/ItemLists')
      },
      {
        path: '/admin/itemUpdate/:id',
        name: '更新页',
        meta: {
          isNav: false,
          needLogin: true,
          roles: ['admin', 'a', 'b']
        },
        component: () => import('@views/ItemUpdate')
      },
      {
        path: '/admin/msgList',
        name: '消息中心',
        meta: {
          isNav: true,
          needLogin: true,
          roles: ['admin', 'a', 'b']
        },
        icon: 'area-chart',
        component: () => import('@views/MsgList')
      },
      {
        path: '/admin/setting',
        name: '设置页',
        meta: {
          isNav: true,
          needLogin: true,
          roles: ['a', 'b']
        },
        icon: 'setting',
        component: () => import('@views/Setting')
      }
    ]
  },
  {
    path: '/login',
    name: '登录页',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '@views/Login')
  },
  {
    path: '/nopermission',
    name: 'nopermission',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '@views/Nopermission')
  },
  {
    path: '*',
    name: 'NotFound',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '@views/NotFound')
  }

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

const userInfo = localStorage.getItem('userInfo')
  ? JSON.parse(localStorage.getItem('userInfo'))
  : {}

router.beforeEach((to, from, next) => {
  if (to.meta.needLogin) {
    if (isLogin()) {
      if (!userInfo.role) {
        next()
      } else {
        if (to.meta.roles.includes(userInfo.role)) {
          next()
        } else {
          next('/nopermission')
        }
      }
    } else {
      next('/login')
    }
  } else {
    next()
  }
})

export default router
